package org.yray.license;

import de.schlichtherle.license.*;
import lombok.extern.slf4j.Slf4j;

import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.util.Date;
import java.util.prefs.Preferences;

@Slf4j
public class LicenseCreator {
    private static final X500Principal DEFAULT_HOLDER_AND_ISSUER =
    new X500Principal("CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN");

    public static void main(String[] args){
        LicenseCreatorParam param =new LicenseCreatorParam();
        param.setSubject("license");
        param.setPrivateAlias("privateKey");
        param.setKeyPass("myKeyPass123");
        param.setStorePass("myStorePass123");
        param.setLicensePath("license.lic");
        param.setPrivateKeysStorePath("privateKeys.keystore");
        param.setIssuedTime(new Date());
        param.setExpiryTime(new Date(System.currentTimeMillis()+10L*365*24*60*60*1000));
        param.setConsumerType("user");
        param.setConsumerAmount(1);
        param.setDescription("This is a license file.");

        try{
            new LicenseCreator().generateLicense(param);
            log.info("License file generated successfully at: {}", param.getLicensePath());
        }catch(Exception e){
            log.error("Failed to generate license file", e);
        }
    }

    public void generateLicense(LicenseCreatorParam param){
        try{
            LicenseManager licenseManager =new LicenseManager(initLicenseParam(param));
            LicenseContent licenseContent =initLicenseContent(param);
            licenseManager.store(licenseContent,new File(param.getLicensePath()));
        }catch(Exception e){
            log.error("License generation failed", e);
        }
    }

    private static LicenseParam initLicenseParam(LicenseCreatorParam param){
        Preferences preferences =Preferences.userNodeForPackage(LicenseCreator.class);
        CipherParam cipherParam =new DefaultCipherParam(param.getStorePass());
        KeyStoreParam privateStoreParam =new CustomKeyStoreParam(
                LicenseCreator.class,
                param.getPrivateKeysStorePath(),
                param.getPrivateAlias(),
                param.getStorePass(),
                param.getKeyPass()
        );
        return new DefaultLicenseParam(param.getSubject(), preferences, privateStoreParam, cipherParam);
    }

    private static LicenseContent initLicenseContent(LicenseCreatorParam param){
        LicenseContent content =new LicenseContent();
        content.setHolder(DEFAULT_HOLDER_AND_ISSUER);
        content.setIssuer(DEFAULT_HOLDER_AND_ISSUER);
        content.setSubject(param.getSubject());
        content.setIssued(param.getIssuedTime());
        content.setNotBefore(param.getIssuedTime());
        content.setNotAfter(param.getExpiryTime());
        content.setConsumerType(param.getConsumerType());
        content.setConsumerAmount(param.getConsumerAmount());
        content.setInfo(param.getDescription());
        return content;
    }
}
